package com.bc.bcapi.service;

import com.bc.bcmodel.domain.T_Erc_contractModel;
import com.bc.bcmodel.domain.T_Wallet_infoModel;
import com.bc.bcmodel.req.AccountDryWithDrawReq;
import com.bc.bcmodel.req.NewOutWalletReq;
import com.bc.bcmodel.req.UpdateOutWalletReq;
import com.bc.bcmodel.resp.WalletTokenResp;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_User_mytokenMapper;
import com.bc.bcmodel.domain.T_User_mytokenModel;

import com.bc.bcapi.dao.T_Erc_contractMapper;

/**
 * @Name T_User_mytokenService * @Description TODO * @Author generation * @Date 2018/8/9 15:56:31 * @Version 1.0
 **/
@Service
public class T_User_mytokenService {
    @Resource
    T_User_mytokenMapper t_User_mytokenMapper;

    /**
     * 保存单条数据     *      * @return
     */
    
    public int insert(T_User_mytokenModel req) {
        return t_User_mytokenMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */
    
    public int insertBatch(List<T_User_mytokenModel> req) {
        return t_User_mytokenMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    
    public T_User_mytokenModel findByNamedParam(T_User_mytokenModel req) {
        return t_User_mytokenMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    
    public List<T_User_mytokenModel> getAllList(T_User_mytokenModel req) {
        return t_User_mytokenMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    
    public PageInfo<T_User_mytokenModel> getList(T_User_mytokenModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_User_mytokenModel> list = t_User_mytokenMapper.findByNamedParamList(req);
        return new PageInfo<T_User_mytokenModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */
    
    public int update(T_User_mytokenModel req) {
        return t_User_mytokenMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */
    
    public int delete(BigInteger id) {
        return t_User_mytokenMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    
    public long count(T_User_mytokenModel req) {
        return t_User_mytokenMapper.count(req);
    }

    public List<T_User_mytokenModel> getUserInuseOutWallet(String uid) {
        T_User_mytokenModel t_user_mytokenModel = new T_User_mytokenModel();
        t_user_mytokenModel.setInuse("1");
        t_user_mytokenModel.setUser_id(uid);
        return t_User_mytokenMapper.findByNamedParamList(t_user_mytokenModel);
    }

    public JsonResult create(NewOutWalletReq req, String uid) {
        HttpReturnRnums paramsCheckEnum = req.paramsCheck();
        if (paramsCheckEnum != HttpReturnRnums.Success) {
            return new JsonResult(paramsCheckEnum);
        }

        T_User_mytokenModel t_user_mytokenModel = new T_User_mytokenModel();
        t_user_mytokenModel.setInuse("1");
        t_user_mytokenModel.setUser_id(uid);
        t_user_mytokenModel.setMywallet_url(req.getMywallet_url());
        t_user_mytokenModel.setMemo(req.getMemo());
        t_User_mytokenMapper.insert(t_user_mytokenModel);

        return new JsonResult(t_user_mytokenModel);
    }

    public JsonResult updateWallet(UpdateOutWalletReq req, String uid) {
        HttpReturnRnums paramsCheckEnum = req.paramsCheck();
        if (paramsCheckEnum != HttpReturnRnums.Success) {
            return new JsonResult(paramsCheckEnum);
        }

        T_User_mytokenModel t_user_mytokenModel = new T_User_mytokenModel();
        t_user_mytokenModel.setId(req.getId());
        t_user_mytokenModel.setInuse("1");
        t_user_mytokenModel.setUser_id(uid);

        t_user_mytokenModel = t_User_mytokenMapper.findByNamedParam(t_user_mytokenModel);

        if (t_user_mytokenModel == null) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }

        t_user_mytokenModel.setMywallet_url(req.getMywallet_url());
        t_user_mytokenModel.setMemo(req.getMemo());

        if (t_User_mytokenMapper.update(t_user_mytokenModel) < 1) {
            return new JsonResult(HttpReturnRnums.SystemError);
        }

        return new JsonResult(t_user_mytokenModel);

    }

    public JsonResult deleteWallet(UpdateOutWalletReq req, String uid) {
        HttpReturnRnums paramsCheckEnum = req.paramsCheck();
        if (paramsCheckEnum != HttpReturnRnums.Success) {
            return new JsonResult(paramsCheckEnum);
        }

        T_User_mytokenModel t_user_mytokenModel = new T_User_mytokenModel();
        t_user_mytokenModel.setId(req.getId());
        t_user_mytokenModel.setInuse("1");
        t_user_mytokenModel.setUser_id(uid);

        t_user_mytokenModel = t_User_mytokenMapper.findByNamedParam(t_user_mytokenModel);

        if (t_user_mytokenModel == null) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }

        t_user_mytokenModel.setInuse("0");

        if (t_User_mytokenMapper.update(t_user_mytokenModel) < 1) {
            return new JsonResult(HttpReturnRnums.SystemError);
        }

        return new JsonResult(t_user_mytokenModel);
    }

    public JsonResult getUserAllToken(String uid) {
        List<WalletTokenResp> resp = t_User_mytokenMapper.getUserAllToken(uid);
        return new JsonResult(resp);
    }

    public JsonResult getById(String uid, BigInteger id) {
        T_User_mytokenModel t_user_mytokenModel = t_User_mytokenMapper.selectByPrimaryKey(id);
        if (t_user_mytokenModel == null) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }

        if (!uid.equals(t_user_mytokenModel.getUser_id())) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }

        return new JsonResult(t_user_mytokenModel);
    }

}